<?php
namespace app\common\exception;


use think\Exception;
use Throwable;

class BaseException extends Exception
{
    //HTTP状态码
    public $code = 400;

    //错误具体信息
    public $msg = '参数错误';

    public function __construct($params = [])
    {
        if(!is_array($params)){
            return;
        }
        if(array_key_exists('code',$params)){
            $this->code = $params['code'];
            $errorCode = $this->errorCode();
            if (!empty($errorCode[$this->code]))
            {
                $this->msg = $errorCode[$this->code];
            } else {
                $this->msg = $params['msg'] ?? '系统繁忙，请稍候再试';
            }
        }
    }

    public function errorCode()
    {
        $codeList = [
            0 => '请求成功',
            400 => '参数错误',
            401 => '系统繁忙，请稍候再试',
            1001 => 'token错误',
            1002 => '不合法的用户信息',
            1003 => '验证码错误',
            1004 => '没有访问权限',
        ];
        return $codeList;
    }
}